package com.qf.java2107.yb.hotel.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.qf.java2107.yb.hotel.common.ResultVO;
import com.qf.java2107.yb.hotel.constant.MessageConstant;
import com.qf.java2107.yb.hotel.dao.FoodDao;
import com.qf.java2107.yb.hotel.dao.FoodTypeDao;
import com.qf.java2107.yb.hotel.entity.BeanFactory;
import com.qf.java2107.yb.hotel.entity.PageBean;
import com.qf.java2107.yb.hotel.pojo.Food;
import com.qf.java2107.yb.hotel.pojo.FoodType;
import com.qf.java2107.yb.hotel.service.FrontService;

import java.sql.SQLException;
import java.util.List;

/**
 * @author yb
 * @version 1.0
 * @date 2021-10-26
 **/
public class FrontServiceImpl implements FrontService {

    private FoodTypeDao foodTypeDao = (FoodTypeDao) BeanFactory.getBean("foodTypeDao");

    private FoodDao foodDao = (FoodDao) BeanFactory.getBean("foodDao");

    @Override
    public ResultVO loadFoodTypeList() {
        try {
            List<FoodType> foodTypeList = foodTypeDao.findAll();
            return ResultVO.ok(MessageConstant.FOODTYPE_QUERY_SUCCESS, foodTypeList);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.FOODTYPE_QUERY_FAIL, e.getMessage());
        }
    }

    /**
     * 分页方法
     * @param currentPage
     * @param pageSize
     * @param typeId
     * @param foodName
     * @return
     */
    @Override
    public ResultVO showFoodListPage(String currentPage, String pageSize, Integer typeId, String foodName) {

        try {
            if(StringUtils.isEmpty(currentPage) || Integer.parseInt(currentPage) <= 0) {
                currentPage = "1";
            }

            if(StringUtils.isEmpty(pageSize) || Integer.parseInt(pageSize) <= 0) {
                pageSize = "6";
            }

            if(StringUtils.isEmpty(foodName)) {
                foodName = "%%";
            } else {
                foodName = "%" + foodName.trim() + "%";
            }

            //封装PageBean
            PageBean<Food> pageBean = new PageBean<>();
            //页码
            pageBean.setCurrentPage(Integer.parseInt(currentPage));
            //页大小
            pageBean.setPageSize(Integer.parseInt(pageSize));

            //分页基数
            int start = (pageBean.getCurrentPage()-1) * pageBean.getPageSize();

            Food food = new Food();
            //封装菜品名称

            //封装菜系ID
            List<Food> foods = foodDao.findByCondition(start, pageBean.getPageSize(), typeId, foodName);
            pageBean.setList(foods);

            Long count = foodDao.findByConditionCount(typeId, foodName);
            pageBean.setTotalCounts(count);

            pageBean.setTotalPages();

            return ResultVO.ok("前台分页成功", pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.error("前台分页失败", e.getMessage());
        }
    }

    @Override
    public ResultVO findFoodById(Integer foodId) {
        try {
            Food food = foodDao.findById(foodId);
            return ResultVO.ok("查询成功",food);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResultVO.error("查询失败");
    }
}
